//
//  NewMissonListDetailButtomView.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2018/3/27.
//  Copyright © 2018年 zjhcsoftios. All rights reserved.
//

#import "NewMissonListDetailButtomView.h"
#import <MJExtension/MJExtension.h>
#import "NetworkAddress.h"
#import "UIColor+EX.h"
#import "iSeeNetworkRequest.h"

@interface NewMissonListDetailButtomView()

@property (nonatomic, strong) NSMutableArray *dataList;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, assign) CGFloat buttonWidth;

@end


@implementation NewMissonListDetailButtomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _dataList = [NSMutableArray array];
        _buttonWidth = (kScreenWidth - 75) / 4;
        UIView *colorView = [[UIView alloc] init];
        [colorView setFrame:CGRectMake(0 , 0, kScreenWidth, 1)];
        [self addSubview:colorView];
        
        colorView.backgroundColor = [UIColor colorWithHexString:@"DBDDE1"];

    }
    return self;
}


- (void)setLogId:(NSString *)logId {
    _logId = logId;
    
    [self getDetailButtomList];
}

- (void)getDetailButtomList {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    
    if (_isOrdorListEnter) {
    //订单进入
        [params setObject:@"market_bill_trade" forKey:@"viewType"];
        if (self.detailModel.orderId) {
            [params setObject:self.detailModel.orderId forKey:@"orderId"];
        }
    }else {
    //任务单进入
        if([_logId isEqualToString:@"526"] || [_logId isEqualToString:@"548"]){
    //抢单池和抢单池组
            [params setObject:@"market_bill_qd" forKey:@"viewType"];
            
            if (self.detailModel.orderId) {
                [params setObject:self.detailModel.orderId forKey:@"orderId"];
            }
            
        }else {
    //其他任务单
            [params setObject:@"market_button" forKey:@"viewType"];
            if (self.detailModel.orderId) {
                [params setObject:self.detailModel.orderId forKey:@"orderId"];
            }
        }
    }
    

    [_dataList removeAllObjects];
    
    [params setObject:self.logId forKey:@"logId"];
    
    [iSeeNetworkRequest getWithHeaderUrl:kFormat(@"%@%@",NMarketMainUrl,ISEE_URL_GET_HOMEENTER_LIST) params:params success:^(id object) {
        [self.dataList addObjectsFromArray:[AppModuleModel mj_objectArrayWithKeyValuesArray:object[@"body"][@"userInfos"]]];
        
        [self buttonsInitWithDataList];
        
    } failure:^(NSError *error) {
        
    }];

}

#pragma mark ButtonInit

- (void)buttonsInitWithDataList {
    if (_dataList.count <= 4) {
        for (int i = 0; i < _dataList.count; i++) {

            UILabel *label = [self viewWithTag:i + 100];
            AppModuleModel *model = _dataList[i];
            if (!label) {
                
                UILabel *label = [self getButtonFromButtonModel:model];
                [self addSubview:label];
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(self);
                    make.right.equalTo(self.mas_right).with.offset(- ((_buttonWidth + 15) * i + 15));
                    //                make.width.mas_equalTo(_buttonWidth);
                    //                make.height.mas_equalTo(@29);
                }];
                label.tag = 100 + i;
            }else {
                
                label.text = model.name;
            }
            
            
            
        }
    }else {
        
        AppModuleModel *moreModel = [[AppModuleModel alloc] init];
        moreModel.styleType = @"2";
        moreModel.styleColor = @"169BD5";
        moreModel.name = @"更多";
        [_dataList insertObject:moreModel atIndex:0];
        
        
        for (int i = 0; i < 4; i++) {
            
            UILabel *label = [self viewWithTag:i + 100];
            AppModuleModel *model = _dataList[i];
            if (!label) {
                
                UILabel *label = [self getButtonFromButtonModel:model];
                [self addSubview:label];
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(self);
                    make.right.equalTo(self.mas_right).with.offset(- ((_buttonWidth + 15) * i + 15));

                }];
                
                label.tag = 100 + i;
            }else {
                
                label.text = model.name;
            }
            
        }
    }
}


#pragma mark button

- (UILabel *)getButtonFromButtonModel:(AppModuleModel *)model {
    
    UILabel *label = [[UILabel alloc] init];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@(_buttonWidth));
        make.height.mas_equalTo(@29);
    }];
    
    label.userInteractionEnabled = YES;
    label.layer.masksToBounds = YES;
    label.layer.borderWidth = 1;
    label.layer.cornerRadius = 2;
    label.text = model.name;
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    
    if ([model.styleType isEqualToString:@"1"]) {
        if (model.styleColor.length > 0) {
        
            label.backgroundColor = [UIColor colorWithHexString:model.styleColor];
            label.layer.borderColor = [UIColor colorWithHexString:model.styleColor].CGColor;
            label.textColor = [UIColor whiteColor];
        }else {
            
            label.backgroundColor = [UIColor colorWithHexString:@"169BD5"];
            label.layer.borderColor = [UIColor colorWithHexString:@"169BD5"].CGColor;
            label.textColor = [UIColor whiteColor];
        }

        label.textColor = [UIColor whiteColor];
    }else if ([model.styleType isEqualToString:@"2"]) {
        if (model.styleColor.length > 0) {
            label.layer.borderColor = [UIColor colorWithHexString:model.styleColor].CGColor;
            label.textColor = [UIColor colorWithHexString:model.styleColor];
        }else {
            label.layer.borderColor = [UIColor colorWithHexString:@"169BD5"].CGColor;
            label.textColor = [UIColor colorWithHexString:@"169BD5"];
        }
    }
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonAction:)];
    [label addGestureRecognizer:tap];
    
    
    
    return label;
}

- (void)buttonAction:(UITapGestureRecognizer *) tap {
    if ([_delegate respondsToSelector:@selector(NewMissionListDetailButtomViewButtonAction:MoreList:)]) {
        AppModuleModel *model = _dataList[tap.view.tag - 100];
        if ([model.name isEqualToString:@"更多"]) {
            NSArray *temp = [_dataList subarrayWithRange:NSMakeRange(4, _dataList.count - 4)];
            [_delegate NewMissionListDetailButtomViewButtonAction:nil MoreList:temp];
        }else {
            [_delegate NewMissionListDetailButtomViewButtonAction:model MoreList:nil];
        }

    }
}










@end
